package cn.xm.test3.web.controller;

import cn.xm.result.PageList;
import cn.xm.test3.domain.Typedictionary;
import cn.xm.test3.query.TypedictionaryQuery;
import cn.xm.test3.service.ITypedictionaryService;
import cn.xm.result.AjaxResult;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/typedictionary")
public class TypedictionaryController {

    @Autowired
    public ITypedictionaryService typedictionaryService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult saveOrUpdate(@RequestBody Typedictionary typedictionary){
        if(typedictionary.getId()!=null){
            typedictionaryService.updateById(typedictionary);
        }else{
            typedictionaryService.insert(typedictionary);
        }
        return AjaxResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        typedictionaryService.deleteById(id);
        return AjaxResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("id")Long id){
        return AjaxResult.success(typedictionaryService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public AjaxResult list(){
        return AjaxResult.success(typedictionaryService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public AjaxResult page(@RequestBody TypedictionaryQuery query){
        Page<Typedictionary> page = new Page<Typedictionary>(query.getPage(),query.getRows());
        page = typedictionaryService.selectPage(page);
        return AjaxResult.success(new PageList<Typedictionary>(page.getTotal(),page.getRecords()));
    }
}
